package com.cj.core.response;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description
 * @Author 陈军（tel:13984919416）
 * @Date 2022年05月13日 8:46
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常捕捉
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public R<?> exceptionGet(Exception e) {
        R r;
        if (e instanceof CJExceptionHandle) {
            // 自定义异常
            CJExceptionHandle applicationException = (CJExceptionHandle) e;
            r = RUtil.error(JSONObject.parseObject(applicationException.getMessage(), REnum.class));
        } else {
            r = RUtil.msg(REnum.FAIL, e.getMessage());
        }
        return r;
    }
}
